// 选择发货单组件,注意要用反引号
Vue.component('invoice-select', {
    template: `<div> 
            <el-form-item label="目标发货单" required class="indent-code">
                    <el-popover placement="right" width="800" trigger="click" v-model="invPopover">
                            <div style="margin-bottom: 5px">
                                <el-input v-model="invoiceSearch.searchVal" placeholder="搜索信息" clearable
                                          size="mini"></el-input>
                                <el-button type="success" icon="el-icon-search" @click="searchInvoice" size="mini">查询</el-button>
                            </div>
                            <el-table :data="invoiceList" max-height="250" border
                                      row-class-name="invoice-table-row" v-loading="invoiceSearch.loading"
                                      highlight-current-row @current-change="handleInvChange">
                                <el-table-column width="210" property="code" label="发货单编号"></el-table-column>
                                <el-table-column width="100" property="carCode" label="车号"></el-table-column>
                                <el-table-column width="100" property="sendStere" label="发货方量"></el-table-column>
                                <el-table-column width="150" property="conName" label="工程名称"
                                                 show-overflow-tooltip></el-table-column>
                                <el-table-column width="150" property="clientName" label="客户名称"
                                                 show-overflow-tooltip></el-table-column>
                                <el-table-column width="150" property="site" label="施工部位"
                                                 show-overflow-tooltip></el-table-column>
                                <el-table-column width="150" property="createTypeTxt" label="创建方式"
                                                 show-overflow-tooltip></el-table-column>
                                <template v-if="isLinked == 1">
                                    <el-table-column width="150" label="是否关联" >
                                        <template scope="scope">
                                            <template v-if="scope.row.controlInvoiceId != null">
                                                已关联
                                            </template>
                                            <template v-else>
                                                未关联
                                            </template>
                                        </template>
                                    </el-table-column>    
                                </template>
                            </el-table>
                        <el-input slot="reference" v-model="formExhibition.invoiceCode" readonly placeholder="点击选择发货单" :disabled="canEdit"></el-input>
                    </el-popover>
                </el-form-item>
                <el-form-item label="施工部位" class="indent-site">
                    <el-input v-model="formExhibition.site" readonly></el-input>
                </el-form-item>
                <el-form-item label="合同名称">
                    <el-input v-model="formExhibition.conName" readonly></el-input>
                </el-form-item>
                <el-form-item label="客户名称">
                    <el-input v-model="formExhibition.clientName" readonly></el-input>
                </el-form-item>
                <el-form-item label="施工单位">
                    <el-input v-model="formExhibition.builder" readonly></el-input>
                </el-form-item>
                <el-form-item label="施工部位">
                    <el-input v-model="formExhibition.site" readonly></el-input>
                </el-form-item>
                <el-form-item label="发货方量" class="send-stere">
                    <el-input v-model="formExhibition.sendStere" readonly></el-input>
                </el-form-item>
                <el-form-item label="生产方量" class="send-stere">
                    <el-input v-model="formExhibition.proStere" readonly></el-input>
                </el-form-item>
                <br>
                <el-form-item label="强度等级">
                    <el-input v-model="formExhibition.strengthName" readonly></el-input>
                </el-form-item>
                <el-form-item label="塌落度">
                    <el-input v-model="formExhibition.slumps" readonly></el-input>
                </el-form-item>
                <el-form-item label="特性要求">
                    <el-input v-model="formExhibition.tech" readonly></el-input>
                </el-form-item>
                <el-form-item label="浇筑方式">
                    <el-input v-model="formExhibition.castModelName" readonly></el-input>
                </el-form-item>
           </div>
      `,
    props: {
        invoiceType: null,
        //  用于展示是否关联工控机发货单
        isLinked: null,
        //  发货单id
        invoiceId: null,
        //  是否可以修改
        canEdit: null,
    },
    data() {
        return {
            invPopover: false,
            //  任务单列表
            invoiceList: [],
            //  任务单搜索框
            invoiceSearch: {
                pageIndex: 0,
                limit: 50,
                searchVal: "",
                loading: false,
                createType: null,
                isLinked: null,
            },
            //  展示数据
            formExhibition: {}
        };
    },
    created() {
        let that = this;
        let invoiceId = that.invoiceId;
        if (invoiceId != null) {
            //  加载发货单
            that.loadInvDetail();
        } else {
            that.searchInvoice();
        }
    },
    mounted() {
        let that = this;
    },
    methods: {
        loadInvDetail() {
            let that = this;
            $.ajax({
                type: "POST",
                url: "/pro/invoice/detail/" + that.invoiceId,
                async: true,
                dataType: 'json',
                success: function (res) {
                    if (res.code == 0) {
                        that.formExhibition = res.data;
                        that.formExhibition.invoiceCode = res.data.code;
                    } else {
                        that.$message({
                            showClose: true,
                            message: "查询发货单详情失败:" + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    // that.invoiceDialog.loading = false;
                }
            });
        },
        //  查询发货单数据
        searchInvoice() {
            let that = this;
            that.invoiceSearch.loading = true;
            if (that.invoiceType != null) {
                that.invoiceSearch.createType = that.invoiceType;
            }
            if (that.isLinked != null) {
                that.invoiceSearch.isLinked = that.isLinked;
            }
            $.ajax({
                type: "POST",
                url: "/pro/invoice/page",
                async: true,
                dataType: 'json',
                data: that.invoiceSearch,
                success: function (res) {
                    if (res.code == 0) {
                        let page = res.data;
                        that.invoiceList = page.records;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询发货单失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.invoiceSearch.loading = false;
                }
            })
        },
        //  选择了发货单
        handleInvChange(val) {
            let that = this;
            if (isNull(val)) {
                return;
            }
            that.formExhibition = val;
            that.formExhibition.invoiceCode = val.code;
            that.formExhibition.sendStere = val.sendStere;
            this.$emit("change", val)
            that.invPopover = false;
        },
    }
});

// 创建 Vue 实例
new Vue({
    el: '#app',
    data() {
        return {};
    }
});
